package com.lujianfei.virusbroadcast.widget;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.util.List;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;

import javax.swing.JPanel;

import com.lujianfei.virusbroadcast.beans.Hospital;
import com.lujianfei.virusbroadcast.beans.Person;
import com.lujianfei.virusbroadcast.cache.SPRepository;
import com.lujianfei.virusbroadcast.factory.PersonPool;
import com.lujianfei.virusbroadcast.factory.PersonPool.PersonCount;

/**
 * 主面板。
 *
 * @ClassName: MyPanel
 * @Description: 主面板
 * @author: Bruce Young
 * @date: 2020年02月02日 17:03
 */
public class MyPanel extends JPanel implements Runnable {

	public MyPanel() {
		super();
		this.setBackground(new Color(0x444444));
	}

	@Override
	public void paint(Graphics g) {
		super.paint(g);
		g.setColor(new Color(0x00ff00));// 设置医院边界颜色
		// 绘制医院边界
		g.drawRect(Hospital.getInstance().getX(), Hospital.getInstance().getY(), Hospital.getInstance().getWidth(),
				Hospital.getInstance().getHeight());
		g.setFont(new Font("微软雅黑", Font.BOLD, 16));
		g.setColor(new Color(0x00ff00));
		g.drawString("医院", Hospital.getInstance().getX() + Hospital.getInstance().getWidth() / 4,
				Hospital.getInstance().getY() - 16);
		// 绘制代表人类的圆点
		List<Person> people = PersonPool.getInstance().getPersonList();
		if (people == null) {
			return;
		}
		for (Person person : people) {
			switch (person.getState()) {
			case Person.State.NORMAL: {
				// 健康人
				g.setColor(new Color(0xdddddd));
				break;
			}
			case Person.State.SHADOW: {
				// 潜伏期感染者
				g.setColor(new Color(0xffee00));
				break;
			}

			case Person.State.CONFIRMED: {
				// 确诊患者
				g.setColor(new Color(0xff0000));
				break;
			}
			case Person.State.FREEZE: {
				// 已隔离者
				g.setColor(new Color(0x48FFFC));
				break;
			}
			case Person.State.DEATH: {
				// 死亡患者

				g.setColor(new Color(0x000000));
				break;
			}
			}
			person.update();// 对各种状态的市民进行不同的处理
			g.fillOval(person.getX(), person.getY(), 3, 3);

		}

		int captionStartOffsetX = Hospital.getInstance().getX() + Hospital.getInstance().getWidth() + 40;
		int captionStartOffsetY = 40;
		int captionSize = 24;

		// 显示数据信息
		g.setColor(Color.WHITE);
		g.drawString("城市总人数：" + SPRepository.getInstance().getCityPersonSize(), captionStartOffsetX, captionStartOffsetY);
		g.setColor(new Color(0xdddddd));
		PersonCount peopleSize = PersonPool.getInstance().getPeopleSize();
		g.drawString("健康者人数：" + peopleSize.getNormalCount(), captionStartOffsetX,
				captionStartOffsetY + captionSize);
		g.setColor(new Color(0xffee00));
		g.drawString("潜伏期人数：" + peopleSize.getShadowCount(), captionStartOffsetX,
				captionStartOffsetY + 2 * captionSize);
		g.setColor(new Color(0xff0000));
		g.drawString("发病者人数：" + peopleSize.getConfirmedCount(), captionStartOffsetX,
				captionStartOffsetY + 3 * captionSize);
		g.setColor(new Color(0x48FFFC));
		g.drawString("已隔离人数：" + peopleSize.getFreezeCount(), captionStartOffsetX,
				captionStartOffsetY + 4 * captionSize);
		g.setColor(new Color(0x00ff00));
		g.drawString("空余病床："
				+ Math.max(SPRepository.getInstance().getBedCount() - peopleSize.getFreezeCount(), 0),
				captionStartOffsetX, captionStartOffsetY + 5 * captionSize);
		g.setColor(new Color(0xE39476));
		// 暂定急需病床数量为 NEED = 确诊发病者数量 - 已隔离住院数量
		int needBeds = peopleSize.getConfirmedCount()
				- peopleSize.getFreezeCount();

		g.drawString("急需病床：" + (needBeds > 0 ? needBeds : 0), captionStartOffsetX,
				captionStartOffsetY + 6 * captionSize);
		g.setColor(new Color(0xccbbcc));
		g.drawString("病死人数：" + peopleSize.getDeathCount(), captionStartOffsetX,
				captionStartOffsetY + 7 * captionSize);
		g.setColor(new Color(0xffffff));
		float infectRatio = 1f - (float)peopleSize.getNormalCount() / SPRepository.getInstance().getCityPersonSize();
		g.drawString(String.format("感染比例：%.0f %%",infectRatio * 100), captionStartOffsetX,
				captionStartOffsetY + 8 * captionSize);
		g.setColor(new Color(0xffffff));
		g.drawString("世界时间（天）：" + (int) (worldTime / 10.0), captionStartOffsetX, captionStartOffsetY + 9 * captionSize);

	}

	public static int worldTime = 0;// 世界时间

	private boolean running;

	public Timer timer = new Timer();

    class MyTimerTask extends TimerTask {
        @Override
        public void run() {
            MyPanel.this.repaint();
            worldTime++;
        }
    }
    
	@Override
	public void run() {
		initInfected();
		if (null == timer) {
			timer = new Timer();
		}
		if (running) {
			return;
		}
		initInfected();
		timer.schedule(new MyTimerTask(), 0, 100);//启动世界计时器，时间开始流动（突然脑补DIO台词：時は停た）
		running = true;
	}

	public void cancel() {
		if (null != timer) {
			timer.cancel();
			timer = null;
		}
		running = false;
	}
	
	public void reset() {
		Hospital.getInstance().resetBedPosition();
		PersonPool.getInstance().reset();
		worldTime = 0;
		repaint();
	}

	public boolean isRunning() {
		return running;
	}
	
	/**
     * 初始化初始感染者
     */
    private void initInfected() {
        List<Person> people = PersonPool.getInstance().getPersonList();//获取所有的市民
        for (int i = 0; i < SPRepository.getInstance().getOriginalCount(); i++) {
            Person person;
            do {
                person = people.get(new Random().nextInt(people.size() - 1));//随机挑选一个市民
            } while (person.isInfected());//如果该市民已经被感染，重新挑选
            person.beInfected();//让这个幸运的市民成为感染者
        }
    }
}
